package com.ruoyi.qsng.service.impl;

import com.ruoyi.common.core.domain.AjaxResults;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.SnowFlakeUtil;
import com.ruoyi.qsng.domain.*;
import com.ruoyi.qsng.mapper.*;
import com.ruoyi.qsng.service.IKMyExchangeService;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * 商品-我的兑换Service业务层处理
 *
 * @author kanglou
 * @date 2023-06-07
 */
@Service
public class KMyExchangeServiceImpl implements IKMyExchangeService {
    @Autowired
    private KMyExchangeMapper kMyExchangeMapper;

    @Autowired
    private KIntegralOrderMapper kIntegralOrderMapper;

    @Autowired
    private KShoppingMallMapper kShoppingMallMapper;

    @Autowired
    private KIntegralDetailedMapper kIntegralDetailedMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private KWriteOffMapper kWriteOffMapper;

    @Override
    @Transactional
    public AjaxResults updateStatusById(String id) {

        KMyExchange kMyExchange = kMyExchangeMapper.selectKMyExchangeById(id);
        if (kMyExchange.getIsExchange() == 1) {
            return AjaxResults.error("该商品已核销！");
        }
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
        kMyExchange.setIsExchange((long) 1);

        //往核销表添加数据
        KWriteOff kWriteOff = new KWriteOff();
        kWriteOff.setUserId(String.valueOf(userId));
        kWriteOff.setVerificationId(id);
        String ids = SnowFlakeUtil.getNextId();
        kWriteOff.setId(ids);
        kWriteOffMapper.insertKWriteOff(kWriteOff);
        kMyExchange.setUpdateTime(DateUtils.getNowDate());
        return AjaxResults.success(kMyExchangeMapper.updateKMyExchange(kMyExchange));
    }

    /**
     * 查询商品-我的兑换
     *
     * @param id 商品-我的兑换主键
     * @return 商品-我的兑换
     */
    @Override
    public KMyExchange selectKMyExchangeById(String id) {
        return kMyExchangeMapper.selectKMyExchangeById(id);
    }

    /**
     * 查询商品-我的兑换列表
     *
     * @param kMyExchange 商品-我的兑换
     * @return 商品-我的兑换
     */
    @Override
    public List<KMyExchange> selectKMyExchangeList(KMyExchange kMyExchange) {
        return kMyExchangeMapper.selectKMyExchangeList(kMyExchange);
    }

    /**
     * 查询商品-我的兑换列表数据
     *
     * @param kMyExchange 商品-我的兑换
     * @return 商品-我的兑换集合
     */
    @Override
    public List<KMyExchange> selectExchangeDataList(KMyExchange kMyExchange) {
        return kMyExchangeMapper.selectExchangeDataList(kMyExchange);
    }

    /**
     * 新增商品-我的兑换
     *
     * @param kMyExchange 商品-我的兑换
     * @return 结果
     */
    @Transactional
    @Override
    public int insertKMyExchange(KMyExchange kMyExchange) {
        kMyExchange.setId(SnowFlakeUtil.getNextId());
        kMyExchange.setCreateTime(DateUtils.getNowDate());
        return kMyExchangeMapper.insertKMyExchange(kMyExchange);
    }

    @Override
    public AjaxResults insertKMyExchangeForApp(KMyExchange kMyExchange) {
        //先判断用户积分是否足够
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
        KIntegralOrder kIntegralOrder = kIntegralOrderMapper.selectKIntegralOrderByUserId(String.valueOf(userId));
        KShoppingMall kShoppingMall = kShoppingMallMapper.selectKShoppingMallById(kMyExchange.getCommodityId());
        if (null == kIntegralOrder) {
            return AjaxResults.error("您还没有积分，请先获取积分！");
        }

        if (kIntegralOrder.getIntegral() < kShoppingMall.getIntegral()) {
            return AjaxResults.error("您的积分不足！");
        }

        //判断商品库存
        if (kShoppingMall.getInventory() < 1) {
            return AjaxResults.error("商品库存不足！");
        }
        //更新库存
        kShoppingMall.setInventory(kShoppingMall.getInventory()-1);
        kShoppingMallMapper.updateKShoppingMall(kShoppingMall);

        //扣除积分
        kIntegralOrder.setIntegral(kIntegralOrder.getIntegral() - kShoppingMall.getIntegral());
        kIntegralOrderMapper.updateKIntegralOrder(kIntegralOrder);

        //生成积分记录
        KIntegralDetailed kIntegralDetailed = new KIntegralDetailed();
        kIntegralDetailed.setIntegral(kShoppingMall.getIntegral());
        kIntegralDetailed.setType(String.valueOf(11));
        kIntegralDetailed.setUserId(String.valueOf(userId));
        kIntegralDetailed.setIntegralSource("兑换商品");
        kIntegralDetailed.setCreateTime(new Date());
        kIntegralDetailed.setCreateBy(String.valueOf(userId));
        String id = SnowFlakeUtil.getNextId();
        kIntegralDetailed.setId(id);
        kIntegralDetailedMapper.insertKIntegralDetailed(kIntegralDetailed);

        //生成兑换记录
        kMyExchange.setId(SnowFlakeUtil.getNextId());
        kMyExchange.setCreateTime(DateUtils.getNowDate());
        kMyExchange.setUserId(String.valueOf(userId));
        return AjaxResults.success(kMyExchangeMapper.insertKMyExchange(kMyExchange));
    }

    /**
     * 修改商品-我的兑换
     *
     * @param kMyExchange 商品-我的兑换
     * @return 结果
     */
    @Override
    public int updateKMyExchange(KMyExchange kMyExchange) {
        kMyExchange.setUpdateTime(DateUtils.getNowDate());
        return kMyExchangeMapper.updateKMyExchange(kMyExchange);
    }

    /**
     * 批量删除商品-我的兑换
     *
     * @param ids 需要删除的商品-我的兑换主键
     * @return 结果
     */
    @Override
    public int deleteKMyExchangeByIds(String[] ids) {
        return kMyExchangeMapper.deleteKMyExchangeByIds(ids);
    }

    /**
     * 删除商品-我的兑换信息
     *
     * @param id 商品-我的兑换主键
     * @return 结果
     */
    @Override
    public int deleteKMyExchangeById(String id) {
        return kMyExchangeMapper.deleteKMyExchangeById(id);
    }
}
